﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TdgMaker.DataType
{
    class RouteData
    {
        public string SName_RouteNr;
        public string IL_Nr;
        public string RouteType;
        public string BaliseNr;
        public int BaliseNrBind;
        public string PassBalise;
        public string PassSwith;
        public string Speeds;
        public string PassTracks;
        public BalisePos bindBalise;
        public TrackData bindTrack;
        public BaliseGroup[] includeBalise;
        public TrackData[] includeTrack;
        public bool isSelected = false;
        public string cal = "+";
        public string sSignal = "";
        public string eSignal = "";

        public void analysisTrack()
        {
            try
            {
                string[] ss = PassTracks.Replace(" ", "").Replace("\n", "").Replace("\t", "").Split(',');
                if (ss[ss.Length - 1] == "")
                {
                    includeTrack = new TrackData[ss.Length - 1];
                }
                else
                {
                    includeTrack = new TrackData[ss.Length];

                }
                for (int i = 0; i < includeTrack.Length; i++)
                {
                    string[] tt = ss[i].Split('\\');
                    includeTrack[i] = new TrackData();
                    includeTrack[i].Length = Convert.ToInt32(tt[0], 10);
                    includeTrack[i].CF = tt[1];
                    includeTrack[i].SignalName = tt[2];
                    includeTrack[i].SetSNameTName(SName_RouteNr.Split('@')[0], tt[3]);
                    includeTrack[i].isInStation = true;

                }
            }
            catch
            {
                System.Console.WriteLine(PassTracks);
            }
        }

        public void UpDateBindLoc(bool isJC, bool isNR)
        {
            if (isJC)
            {
                if (isNR)
                {
                    for (int i = 0; i < includeTrack.Length; i++)
                    {
                        includeTrack[i].TrackLoc = bindTrack.TrackLoc + i;
                    }
                }
                else
                {
                    for (int i = 0; i < includeTrack.Length; i++)
                    {
                        includeTrack[i].TrackLoc = bindTrack.TrackLoc - i;
                    }
                }
            }
            else
            {
                if (isNR)
                {
                    for (int i = 0; i < includeTrack.Length; i++)
                    {
                        includeTrack[i].TrackLoc = bindTrack.TrackLoc - includeTrack.Length + i;
                    }

                }
                else
                {
                    for (int i = 0; i < includeTrack.Length; i++)
                    {
                        includeTrack[i].TrackLoc = bindTrack.TrackLoc + includeTrack.Length - i;
                    }
                }
            }
        }

        public void analysisBalsie(ArrayList BaliseList)
        {
            //019/879,
            //021/258,
            //023/253
            try
            {
                SetBaliseNr(BaliseNr);
                for (int i = 0; i < BaliseList.Count; i++)
                {
                    if (((BalisePos)BaliseList[i]).Balise_Nr==BaliseNrBind )
                    {
                        bindBalise = ((BalisePos)BaliseList[i]);
                    }
                }
                string[] ss = PassBalise.Replace(" ", "").Replace("\n", "").Replace("\t", "").Split(',');
                if (ss[ss.Length - 1] == "")
                {
                    includeBalise = new BaliseGroup[ss.Length - 1];
                }
                else
                {
                    includeBalise = new BaliseGroup[ss.Length];
                }
                int miss = 0;
                for (int i = 0; i < includeBalise.Length; i++)
                {
                    string[] tt = ss[i].Split('/');
                    miss = Convert.ToInt32(tt[1], 10);
                    includeBalise[i] = new BaliseGroup();
                    includeBalise[i].baliseNIDBG = calBaliseNr(bindBalise.Balise_Nr, Convert.ToInt32(tt[0], 10));
                    includeBalise[i].getBaliseFromDB();
                    for (int n = 0; n < includeBalise[i].baliseInGroup.Length; n++)
                    {
                        includeBalise[i].baliseInGroup[n].myPos = bindBalise.myPos +( includeBalise[i].baliseInGroup[n].Balise_Loc-bindBalise.Balise_Loc);
                    }
                }
            }
            catch
            {
                System.Console.WriteLine(PassBalise);
            }
        }

        private int calBaliseNr(int p, int p_2)
        {
            int s3 = (int)((((UInt32)p << (8 + 7 + 3)) >> 26) << 8) + p_2;

            return s3;
        }

        public void SetBaliseNr(string s)
        {
            //071-4-07-013-2(T1)
            string[] ss = s.Split('-', '(', ')');
            int a1 = Convert.ToInt32(ss[0], 10);
            int a2 = Convert.ToInt32(ss[1], 10);
            int a3 = Convert.ToInt32(ss[2], 10);
            int a4 = Convert.ToInt32(ss[3], 10);
            int a5 = 1;
            if (ss.Length >= 5)
            {
                a5 = Convert.ToInt32(ss[4], 10);
            }

            int aa = 0;
            aa += a5;
            aa = aa << 7;
            aa += a1;
            aa = aa << 3;
            aa += a2;
            aa = aa << 6;
            aa += a3;
            aa = aa << 8;
            aa += a4;
            this.BaliseNrBind = aa;
        }
    }
    class BaliseGroup
    {
        public int missDis = 0;
        public int baliseNIDBG = 0;
        public BalisePos[] baliseInGroup;

        public void getBaliseFromDB()
        {
            baliseInGroup = TB_DB.getBaliseGroupFromDB(baliseNIDBG);
        }
    }
}
